package com.tsing.mapper.impl;

import com.tsing.common.framwork.mapper.CommonDao;
import com.tsing.common.result.PageResult;
import com.tsing.mapper.LetterMapper;
import com.tsing.pojo.entity.TlLetter;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴阳基
 * @datetime 2023/12/15 20:51
 */
public class LetterMapperImpl extends CommonDao implements LetterMapper {
    @Override
    public PageResult<TlLetter> list(Integer page, Integer size, Integer userId, Integer senderId) {

        List<TlLetter> list = new ArrayList<>();
        PageResult<TlLetter> pageResult = new PageResult<>();

        int offset = (page - 1) * size;

        try {
            // 修改了查询语句，使用子查询的方式，先按照发送人分组，再按照时间排序，然后分页显示
            list = executeQuery(TlLetter.class,
                    "select * from tl_letter where (user_id = ? and sender_id = ?) or (user_id = ? and sender_id = ?) order by created_time asc",

                    new Object[]{userId, senderId, senderId, userId});
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        pageResult.setCode(200);
        pageResult.setData(list);
        pageResult.setCurrentPage(page);
        pageResult.setSize(size);

        try {
            // 修改了查询语句，使用子查询的方式，先按照发送人分组，再按照时间排序，然后计算总数
            pageResult.setTotal(executeCount("select count(*) from tl_letter where (user_id = ? and sender_id = ?) or (user_id = ? and sender_id = ?)",
                    new Object[]{userId, senderId, senderId, userId,}));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return pageResult;
    }

    @Override
    public Integer send(TlLetter letter) {
        int result = 0;

        try {
            result = executeUpdate("insert into tl_letter(user_id,sender_id,content,created_time) values(?,?,?,?)",
                    new Object[]{letter.getUserId(), letter.getSenderId(), letter.getContent(), letter.getCreatedTime()});

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return null;
    }

    @Override
    public PageResult<TlLetter> latestMessage(Integer page, Integer size, Integer userId) {
        List<TlLetter> list = new ArrayList<>();
        PageResult<TlLetter> pageResult = new PageResult<>();

        int offset = (page - 1) * size;

        try {
            // 修改了查询语句，使用子查询的方式，先按照发送人分组，再按照时间排序，然后分页显示
            list = executeQuery(TlLetter.class,
                    "SELECT `user_id`, `content`,`created_time` FROM (SELECT *, ROW_NUMBER() OVER " +
                            "(PARTITION BY `user_id` ORDER BY `created_time` DESC) as rn FROM `tl_letter`) t WHERE rn" +
                            " = ?;",

                    new Object[]{userId});
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        pageResult.setCode(200);
        pageResult.setData(list);
        pageResult.setCurrentPage(page);
        pageResult.setSize(size);

        try {
            // 修改了查询语句，使用子查询的方式，先按照发送人分组，再按照时间排序，然后计算总数
            pageResult.setTotal(executeCount("SELECT count(1) FROM (SELECT *, ROW_NUMBER() OVER " +
                            "(PARTITION BY `user_id` ORDER BY `created_time` DESC) as rn FROM `tl_letter`) t WHERE rn" +
                            " = ?;",
                    new Object[]{userId}));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return pageResult;
    }

}
